package com.yww.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yww.config.JwtConfig;
import com.yww.entity.Roles;
import com.yww.entity.Users;
import com.yww.mapper.UsersMapper;
import com.yww.service.UsersService;
import com.yww.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class LoginController {
	@Resource
	private JwtConfig jwtConfig;

	@Autowired
	UsersService usersService;
	@Autowired
	UsersMapper usersMapper;




	/*
	 * 返参格式 { "userName": "ID123","token":
	 * "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMSIsImlhdCI6MTU4ODg2MDE0MCwiZXhwIjoxNTg4ODYzNzQwfQ.pwpuI-E2xskyqqzjeWe_DU-ASIyyrwbKswX2da_GC9e2ucZ0xfGSbXdmHQWIZ5ITJXrZLgTtWNBg_ceDivkqtA"
	 * }
	 */
	// 拦截器直接放行，返回Token
	@PostMapping("/login")
	public Map<String, Object> login(@RequestParam("loginName") String loginName,
									 @RequestParam("passWord") String passWord,Users users) {
		Map<String, Object> result = new HashMap<>();

		// 数据源校验
		QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
		String str= MD5.getInstance().getMD5(passWord);
		queryWrapper.eq("login_name", loginName).eq("pass_word", str);
		//根据用户名和密码查询单个对象

		Users one = usersService.getOne(queryWrapper, false);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String lock = usersMapper.getLock(loginName);

		System.out.println(lock);
		if(lock.equals("是")){
			//开始
			List<Roles> nameByName =usersMapper.getNameByName(loginName);
			if(nameByName.size()>0 && nameByName.get(0).getRoleName().contains("管理员")){
				result.put("code", 505);
				result.put("msg", "错误次数过多 由于您是管理员，系统已把您的密码改为管理员密码！");
				usersMapper.updateLockOut(loginName);
				String str1= MD5.getInstance().getMD5("ysd123");
				one.setPassWord(str1);
				usersService.updatePassWord(one);
				usersMapper.updateEmptyWrongNum(loginName);

			}else{
				//判断结束
				result.put("code", 502);
				result.put("msg", "密码错误次数过多，请联系管理员解锁");
			}


		} else if (one == null) {
			usersService.updateWrongNum(loginName);
			Integer wrongNum = usersMapper.getWrongNum(loginName);
			if(wrongNum>=3){
				usersMapper.updateLock(loginName);
				//判断是否为管理员
//				List<Users> nameByName =usersMapper.getNameByName(loginName);
//				if(nameByName.equals("管理员")){
//					System.out.println("ggggggggggggggggggggggggggggggggggggg");
//					usersMapper.updateEmptyWrongNum(loginName);
//					usersMapper.updateLockOut(loginName);
//					usersMapper.updateByName(loginName);
//					result.put("code", 520);
//					result.put("msg", "因为您是管理员 已经把密码重新重置为管理员的默认密码");
//				}

			}
			result.put("code", 501);
			result.put("msg", "登录失败，用户名或密码不正确");
		}else {
			//登录成功之后
			usersMapper.updateLockOut(loginName);
			usersMapper.updateEmptyWrongNum(loginName);
			String token = jwtConfig.getToken(loginName + passWord);
result.put("imgUrl",one.getImgUrl());

			if (!StringUtils.isEmpty(token)) {
				result.put("token",token);
				//session.setAttribute("token",token);
			}

			Date date=new Date();
			usersService.getLastTime(sdf.format(date),loginName);
			result.put("code", 0);
			result.put("data", one);
		}
		return result;
	}

	// 需要 Token 验证的接口
	@PostMapping("/info")
	public String info() {
		return "info";
	}
}
